﻿#ifndef ELEMENTUIREAD_H
#define ELEMENTUIREAD_H
#include <Element/ElementBase.h>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
template<class T>
T* readElementUi(const QString &fileName)
{
    QFile file(fileName);
    file.open(QIODeviceBase::ReadOnly);
    if(!file.isOpen()) return nullptr;
    QByteArray json = file.readAll();
    QJsonDocument  root = QJsonDocument::fromJson(json);
    QJsonObject obj = root.object();
    // lines
    T *ans = new T();
    QJsonArray lines = obj["lines"].toArray();
    for(int i=0;i<lines.count();++i){
        QJsonArray line = lines[i].toArray();
        ans->mLines.push_back(
            QLineF(
                QPointF(line[0].toDouble(),line[1].toDouble()),
                QPointF(line[2].toDouble(),line[3].toDouble())
                )
            );
    }
    // rectangles
    QJsonArray rectangles = obj["rectangles"].toArray();
    for(int i=0;i<rectangles.count();++i){
        QJsonArray rect = rectangles[i].toArray();
        ans->mRects.push_back(
            QRectF(
                rect[0].toDouble(),rect[1].toDouble(),
                rect[2].toDouble(),rect[3].toDouble()
                )
            );
    }
    // hot-spots
    QJsonArray hotSpots = obj["hot-spots"].toArray();
    for(int i=0;i<hotSpots.count();++i){
        QJsonArray hotSpot = hotSpots[i].toArray();
        ans->mPoints.push_back(
            QPointF(hotSpot[0].toDouble(),hotSpot[1].toDouble())
            );
    }
    return true;
}

#endif // ELEMENTUIREAD_H
/*

*/
